<?php
namespace Wx\Controller;
use Think\Controller;
class ArticleController extends CommonController {
    public function index(){
        $this->show('PC端等待资金注入','utf-8');
    }
	
	//贤达搜素
	public function celebrity_ls(){
		$arc_celebrity=D('arc_celebrity');
		$arcli=array();
		$page=1;//第几页
		if($_GET['page'])$page=$_GET['page'];
		//每个乡村
		if(is_numeric($_GET['vid']))$where="v_id=".$_GET['vid'];
		if($_GET['keyword']){
				$arcli=$arc_celebrity->where("title like '%".$_GET['keyword']."%'")->page($page.',8')->order('id desc')->select(); // 查询第一页数据
				$this->assign('keyword',$_GET['keyword']);// 
			}else{
				$arcli=$arc_celebrity->page($page.',8')->where($where)->order('id desc')->select(); // 查询第一页数据 
			}
		
		$user=D('user');
		$user_info=D('user_info');
		//用户信息
		foreach($arcli as $i=>$one){
			$arcli[$i]['arc_id']=$one['id'];//文章id备份
			$arcli[$i]['arc_img']=$one['img'];//文章首页图备份
			$arcli[$i]['arc_supports']=$one['supports'];//点赞
			$arcli[$i]['content']=preg_replace('/<[^>]*>/','',$arcli[$i]['content']);//去掉所有标签
			$getuser=$user->where("id=".$one['u_id'])->find();
			$getinfo=$user_info->where("u_id=".$one['u_id'])->find();
			//提取文章中图片
			$imgarr=array();
			preg_match_all('/<img.*?src="(.*?)".*?>/is',$one['content'],$imgarr);
			$arcli[$i]['img_num']=count($imgarr[1]);//dump($imgarr[1]);
			$arcli[$i]['img_arr']=array_slice($imgarr[1],0,3);//只要前三个图片
			//评论统计
			$comments=D('comments');
			$arcli[$i]['com_num']=$comments->where("arc_type like 'celebrity' and arc_id=".$one['id'])->count();
			$arcli[$i]=array_merge($arcli[$i],(array)$getuser,(array)$getinfo);
		}//end for
		
		//dump($arcli);
		if($_GET['type']){
			$this->ajaxReturn($arcli,'JSON');
			}else{
			$this->assign('arcli',$arcli);//
			$this->display('worthy');
			}
	}//end fun
	//贤达详情
	public function celebrity(){
		$arc_celebrity=D('arc_celebrity');
		if(is_numeric($_GET['id'])){
			$getone=$arc_celebrity->where("id=".$_GET['id'])->find();
			$this->assign('arc',$getone);
			//点击自增
			$arc_celebrity->where("id=".$_GET['id'])->setInc('clicks'); // 阅读加1
			//取出评论
			$comments=D('comments');
			$com=$comments->where("arc_type like 'celebrity' and arc_id=".$_GET['id']." and r_id=0")->order('supports desc')->select();
			$user=D('user');
			$user_info=D('user_info');
			foreach($com as $i=>$one){
				//评论id备份
				$com[$i]['comments_id']=$one['id'];
				$com[$i]['comments_supports']=$one['supports'];
				
				$num=$comments->where("r_id=".$one['id'])->count();
				$com[$i]['num']=$num;
				//用户信息
				$getuser=$user->where("id=".$one['u_id'])->find();
				$getinfo=$user_info->where("u_id=".$one['u_id'])->find();
				$com[$i]=array_merge($com[$i],(array)$getuser,(array)$getinfo);
			}//end for
			$this->assign('com',$com);//dump($com);
			
			$this->display('worthyDetail');
			}else{
				
			}
	}//end
	//点赞文章
	public function celebrity_supports(){
		$arc_celebrity=D('arc_celebrity');
		if(is_numeric($_GET['id'])){
			if($_GET['dowhat']=='add'){
				$arc_celebrity->where("id=".$_GET['id'])->setInc('supports'); // 阅读加1
				}else{
					$arc_celebrity->where("id=".$_GET['id'])->setDec('supports'); // 阅读减1
				}
		}
	}//end fun
	/*********************************************************************************/
	//书记日志
	public function diary(){
		$arc_diary=D('arc_diary');
		if(is_numeric($_GET['id'])){
			$getone=$arc_diary->where("id=".$_GET['id'])->find();
			//日志id备份
			$getone['diary_id']=$getone['id'];
			$getone['diary_supports']=$getone['supports'];
			$getone['diary_clicks']=$getone['clicks'];
			
			$user=D('user');
			$user_info=D('user_info');
			//用户信息
			$getuser=$user->where("id=".$getone['u_id'])->find();
			$getinfo=$user_info->where("u_id=".$getone['u_id'])->find();
			$getone=array_merge($getone,(array)$getuser,(array)$getinfo);
			
			$this->assign('arc',$getone);
			//点击自增
			$arc_diary->where("id=".$_GET['id'])->setInc('clicks'); // 阅读加1
			//取出评论
			$comments=D('comments');
			$com=$comments->where("arc_type like 'diary' and arc_id=".$_GET['id']." and r_id=0")->order('supports desc')->select();
			foreach($com as $i=>$one){
				//评论id备份
				$com[$i]['comments_id']=$one['id'];
				$com[$i]['comments_supports']=$one['supports'];
				
				$num=$comments->where("r_id=".$one['id'])->count();
				$com[$i]['num']=$num;
				//用户信息
				$getuser=$user->where("id=".$one['u_id'])->find();
				$getinfo=$user_info->where("u_id=".$one['u_id'])->find();
				$com[$i]=array_merge($com[$i],(array)$getuser,(array)$getinfo);
			}//end for
			$this->assign('com',$com);//dump($com);
			
			$this->display('journalDetail');
			}else{
				
			}
	}//end
	//点赞文章
	public function diary_supports(){
		$arc_diary=D('arc_diary');
		if(is_numeric($_GET['id'])){
			if($_GET['dowhat']=='add'){
				$arc_diary->where("id=".$_GET['id'])->setInc('supports'); // 阅读加1
				}else{
					$arc_diary->where("id=".$_GET['id'])->setDec('supports'); // 阅读减1
				}
		}
	}//end fun
	/*********************************************************************************/
	//乡村夜话搜索
	public function stories_ls(){
		$arc_stories=D('arc_stories');
		$arcli=array();
		$page=1;//第几页
		if($_GET['page'])$page=$_GET['page'];
		//每个乡村
		if(is_numeric($_GET['vid']))$where="v_id=".$_GET['vid'];
		if($_GET['keyword']){
				$arcli=$arc_stories->where("title like '%".$_GET['keyword']."%'")->page($page.',8')->order('id desc')->select(); // 查询第一页数据
				$this->assign('keyword',$_GET['keyword']);// 
			}else{
				$arcli=$arc_stories->page($page.',8')->where($where)->order('id desc')->select(); // 查询第一页数据
			}
		
		$user=D('user');
		$user_info=D('user_info');
		//用户信息
		foreach($arcli as $i=>$one){
			$arcli[$i]['arc_id']=$one['id'];//文章id备份
			$arcli[$i]['arc_img']=$one['img'];//文章id备份
			$getuser=$user->where("id=".$one['u_id'])->find();
			$getinfo=$user_info->where("u_id=".$one['u_id'])->find();
			//提取文章中图片
			$imgarr=array();
			preg_match_all('/<img.*?src="(.*?)".*?>/is',$one['content'],$imgarr);
			$arcli[$i]['img_num']=count($imgarr[1]);//dump($imgarr[1]);
			$arcli[$i]['img_arr']=array_slice($imgarr[1],0,3);//只要前三个图片
			//评论统计
			$comments=D('comments');
			$arcli[$i]['com_num']=$comments->where("arc_type like 'stories' and arc_id=".$one['id'])->count();
			$arcli[$i]=array_merge($arcli[$i],(array)$getuser,(array)$getinfo);
		}//end for
		
		//dump($arcli);
		if($_GET['type']){
			$this->ajaxReturn($arcli,'JSON');
			}else{
			$this->assign('arcli',$arcli);//
			$this->display('storyList');
			}
	}//end fun
	//乡村夜话
	public function stories(){
		$arc_stories=D('arc_stories');
		if(is_numeric($_GET['id'])){
			$getone=$arc_stories->where("id=".$_GET['id'])->find();
			//日志id备份
			$getone['stories_id']=$getone['id'];
			$getone['stories_supports']=$getone['supports'];
			$getone['stories_clicks']=$getone['clicks'];
			
			$user=D('user');
			$user_info=D('user_info');
			//用户信息
			$getuser=$user->where("id=".$getone['u_id'])->find();
			$getinfo=$user_info->where("u_id=".$getone['u_id'])->find();
			$getone=array_merge($getone,(array)$getuser,(array)$getinfo);
			
			$this->assign('arc',$getone);
			//点击自增
			$arc_stories->where("id=".$_GET['id'])->setInc('clicks'); // 阅读加1
			//取出评论
			$comments=D('comments');
			$com=$comments->where("arc_type like 'stories' and arc_id=".$_GET['id']." and r_id=0")->order('supports desc')->select();
			foreach($com as $i=>$one){
				//评论id备份
				$com[$i]['comments_id']=$one['id'];
				$com[$i]['comments_supports']=$one['supports'];
				
				$num=$comments->where("r_id=".$one['id'])->count();
				$com[$i]['num']=$num;
				//用户信息
				$getuser=$user->where("id=".$one['u_id'])->find();
				$getinfo=$user_info->where("u_id=".$one['u_id'])->find();
				$com[$i]=array_merge($com[$i],(array)$getuser,(array)$getinfo);
			}//end for
			$this->assign('com',$com);//dump($com);
			
			$this->display('storyDetail');
			}else{
				
			}
	}//end
	//点赞文章
	public function stories_supports(){
		$arc_stories=D('arc_stories');
		if(is_numeric($_GET['id'])){
			if($_GET['dowhat']=='add'){
				$arc_stories->where("id=".$_GET['id'])->setInc('supports'); // 阅读加1
				}else{
					$arc_stories->where("id=".$_GET['id'])->setDec('supports'); // 阅读减1
				}
		}
	}//end fun
	/*********************************************************************************/
	//特色产品搜索
	public function farming_ls(){
		$arc_farming=D('arc_farming');
		$arcli=array();
		$page=1;//第几页
		if($_GET['page'])$page=$_GET['page'];
		//每个乡村
		if(is_numeric($_GET['vid']))$where="v_id=".$_GET['vid'];
		if($_GET['keyword']){
				$arcli=$arc_farming->where("title like '%".$_GET['keyword']."%'")->page($page.',8')->order('id desc')->select(); // 查询第一页数据
				$this->assign('keyword',$_GET['keyword']);// 
			}else{
				$arcli=$arc_farming->page($page.',8')->where($where)->order('id desc')->select(); // 查询第一页数据
			}
		
		$user=D('user');
		$user_info=D('user_info');
		//用户信息
		foreach($arcli as $i=>$one){
			$arcli[$i]['arc_id']=$one['id'];//文章id备份
			$arcli[$i]['arc_img']=$one['img'];//文章id备份
			$getuser=$user->where("id=".$one['u_id'])->find();
			$getinfo=$user_info->where("u_id=".$one['u_id'])->find();
			//提取文章中图片
			$imgarr=array();
			preg_match_all('/<img.*?src="(.*?)".*?>/is',$one['content'],$imgarr);
			$arcli[$i]['img_num']=count($imgarr[1]);//dump($imgarr[1]);
			$arcli[$i]['img_arr']=array_slice($imgarr[1],0,3);//只要前三个图片
			//评论统计
			$comments=D('comments');
			$arcli[$i]['com_num']=$comments->where("arc_type like 'farming' and arc_id=".$one['id'])->count();
			$arcli[$i]=array_merge($arcli[$i],(array)$getuser,(array)$getinfo);
		}//end for
		
		//dump($arcli);
		if($_GET['type']){
			$this->ajaxReturn($arcli,'JSON');
			}else{
			$this->assign('arcli',$arcli);//dump($arcli); 
			$this->display('productList');
			}
	}//end fun
	//特色产品
	public function farming(){
		$arc_farming=D('arc_farming');
		if(is_numeric($_GET['id'])){
			$getone=$arc_farming->where("id=".$_GET['id'])->find();
			//id备份
			$getone['farming_id']=$getone['id'];
			$getone['farming_supports']=$getone['supports'];
			$getone['farming_clicks']=$getone['clicks'];
			
			$user=D('user');
			$user_info=D('user_info');
			//用户信息
			$getuser=$user->where("id=".$getone['u_id'])->find();
			$getinfo=$user_info->where("u_id=".$getone['u_id'])->find();
			$getone=array_merge($getone,(array)$getuser,(array)$getinfo);
			
			$this->assign('arc',$getone);
			//点击自增
			$arc_farming->where("id=".$_GET['id'])->setInc('clicks'); // 阅读加1
			//取出评论
			$comments=D('comments');
			$com=$comments->where("arc_type like 'farming' and arc_id=".$_GET['id']." and r_id=0")->order('supports desc')->select();
			foreach($com as $i=>$one){
				//评论id备份
				$com[$i]['comments_id']=$one['id'];
				$com[$i]['comments_supports']=$one['supports'];
				
				$num=$comments->where("r_id=".$one['id'])->count();
				$com[$i]['num']=$num;
				//用户信息
				$getuser=$user->where("id=".$one['u_id'])->find();
				$getinfo=$user_info->where("u_id=".$one['u_id'])->find();
				$com[$i]=array_merge($com[$i],(array)$getuser,(array)$getinfo);
			}//end for
			$this->assign('com',$com);//dump($com);
			
			$this->display('productDetail');
			}else{
				
			}
	}//end
	//点赞文章
	public function farming_supports(){
		$arc_farming=D('arc_farming');
		if(is_numeric($_GET['id'])){
			if($_GET['dowhat']=='add'){
				$arc_farming->where("id=".$_GET['id'])->setInc('supports'); // 阅读加1
				}else{
					$arc_farming->where("id=".$_GET['id'])->setDec('supports'); // 阅读减1
				}
		}
	}//end fun
	/*********************************************************************************/
	//评论点赞
	public function comments_supports(){
		$comments=D('comments');
		if(is_numeric($_GET['id'])){
			if($_GET['dowhat']=='add'){
				$comments->where("id=".$_GET['id'])->setInc('supports'); // 阅读加1
				}else{
					$comments->where("id=".$_GET['id'])->setDec('supports'); // 阅读减1
				}
		}
	}//end fun
	//评论
	public function comments(){
		$comments=D('comments');
		$data['state']=2;
		$data['r_u_id']=$_POST['r_u_id'];
		$data['arc_type']=$_POST['arc_type'];
		$data['arc_id']=$_POST['arc_id'];
		$data['r_id']=$_POST['r_id'];
		$data['content']=$_POST['content'];
		$data['addtime']=time();
		if(session('uid')=='')$this->ajaxReturn('请先登录！');
		$data['u_id']=session('uid');
		$alt=$comments->add($data);
		if($alt>0){
			$this->ajaxReturn('ok');
			}else{
				$this->ajaxReturn('评论失败，请重试！');
			}
	}//end fun
	//评论回复
	public function reply(){
		if(is_numeric($_GET['id'])){
			$comments=D('comments');
			$user=D('user');
			$user_info=D('user_info');
			//该评论信息
			$getone=$comments->where("id=".$_GET['id'])->find();
			$getone['comments_id']=$getone['id'];
			$getone['comments_supports']=$getone['supports']; 
			
			//用户信息
			$getuser=$user->where("id=".$getone['u_id'])->find();
			$getinfo=$user_info->where("u_id=".$getone['u_id'])->find();
			$getone=array_merge($getone,(array)$getuser,(array)$getinfo);
			$this->assign('info',$getone);//
			 
			//取出评论
			$com=$comments->where("state!=1 and r_id=".$_GET['id'])->order('id desc')->select();
			foreach($com as $i=>$one){
				//评论id备份
				$com[$i]['comments_id']=$one['id'];
				$com[$i]['comments_supports']=$one['supports'];
				
				$num=$comments->where("r_id=".$one['id'])->count();
				$com[$i]['num']=$num;
				//用户信息
				$getuser=$user->where("id=".$one['u_id'])->find();
				$getinfo=$user_info->where("u_id=".$one['u_id'])->find();
				$com[$i]=array_merge($com[$i],(array)$getuser,(array)$getinfo);
			}//end for
			$this->assign('com',$com);//dump($com);
		}
			$this->display('apply');
	}//end fun

}